package king.dominic.jorc.ui;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.Build;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import king.dominic.jlibrary.view.WindowUtil;
import king.dominic.jorc.core.Bounds;
import king.dominic.jorc.core.ErrorCode;

/**
 * Created by DominicKing on 2018/1/24.
 * ScanBoxView
 */

public class ScanBoxView extends View {
    private boolean remeasure;
    private Rect rect = new Rect();
    private Paint paint = new Paint();
    private Rect avatarBounds = new Rect();
    private Rect nationalEmblemBounds = new Rect();
    private boolean front = true;
    private boolean isShowErrorBounds = false;
    private int errorCode;
    private long firstShowErrorTime;

    public Rect getRect() {
        return rect;
    }

    public ScanBoxView(Context context) {
        super(context);
    }

    public ScanBoxView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public ScanBoxView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public ScanBoxView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    public void setFront(boolean front) {
        this.front = front;
        requestLayout();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        onMeasureScanBoxBounds();
        drawScanFrame(canvas);

        if (front)
            drawAvatarFrame(canvas);
        else
            drawNationalEmblem(canvas);

        drawErrorBounds(canvas);
    }

    private void drawErrorBounds(Canvas canvas) {
        if (!isShowErrorBounds) return;
        if (firstShowErrorTime == 0) firstShowErrorTime = System.currentTimeMillis();
        long currentTime = System.currentTimeMillis();
        if (currentTime - firstShowErrorTime > 10000) {
            isShowErrorBounds = false;
            firstShowErrorTime = 0;
            return;
        }
        if (((currentTime - firstShowErrorTime) / 600) % 2 != 0) {
            invalidate();
            return;
        }
        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(WindowUtil.dip2px(getContext(), 2));
        canvas.drawRect(getErrorBounds(), paint);
        invalidate();
    }

    private Rect getErrorBounds() {
        switch (errorCode) {
            case ErrorCode.NOT_FOUND_NAME:
                return Bounds.name(rect);
            case ErrorCode.NOT_FOUND_SEX:
                return Bounds.sex(rect);
            case ErrorCode.NOT_FOUND_BIRTHDAY:
                return Bounds.birthday(rect);
            case ErrorCode.NOT_FOUND_ADDRESS:
                return Bounds.address(rect);
            case ErrorCode.NOT_FOUND_NUMBER:
                return Bounds.number(rect);
            case ErrorCode.NOT_FOUND_TITLE:
                return Bounds.title(rect);
            case ErrorCode.NOT_FOUND_VISA_OFFICE:
                return Bounds.visaOffice(rect);
            default:
                return rect;
        }
    }

    private void drawNationalEmblem(Canvas canvas) {
        paint.setColor(Color.WHITE);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(WindowUtil.dip2px(getContext(), 1));
        paint.setTextSize(nationalEmblemBounds.width() / 5);
        canvas.drawRect(nationalEmblemBounds, paint);
//        canvas.drawRect(Bounds.visaOffice(rect), paint);
        paint.setStyle(Paint.Style.FILL);
        canvas.drawText("国徽", nationalEmblemBounds.left * 1.3f,
                nationalEmblemBounds.top + nationalEmblemBounds.height() / 2, paint);
    }

    private void drawAvatarFrame(Canvas canvas) {
        paint.setColor(Color.WHITE);
        paint.setStrokeWidth(WindowUtil.dip2px(getContext(), 1));
        canvas.drawLine(avatarBounds.left, avatarBounds.top, avatarBounds.right, avatarBounds.top, paint);
        canvas.drawLine(avatarBounds.left, avatarBounds.top, avatarBounds.left, avatarBounds.bottom, paint);
        canvas.drawLine(avatarBounds.right, avatarBounds.top, avatarBounds.right, avatarBounds.bottom, paint);
        canvas.drawLine(avatarBounds.left, avatarBounds.bottom, avatarBounds.right, avatarBounds.bottom, paint);
    }

    private void drawScanFrame(Canvas canvas) {
        paint.setColor(Color.WHITE);
        paint.setStyle(Paint.Style.STROKE);
        paint.setAntiAlias(true);
        paint.setStrokeWidth(WindowUtil.dip2px(getContext(), 2));
        final int length = (int) (rect.height() * 0.2f);
        final int r = (int) (rect.width() / 8.5f * 0.3);
        final int d = 2 * r;
        //左上角
        canvas.drawArc(new RectF(rect.left, rect.top, rect.left + d, rect.top + d), 180, 90, false, paint);
        canvas.drawLine(rect.left, rect.top + r, rect.left, rect.top + length, paint);
        canvas.drawLine(rect.left + r, rect.top, rect.left + length, rect.top, paint);
        //右上角
        canvas.drawArc(new RectF(rect.right - d, rect.top, rect.right, rect.top + d), -90, 90, false, paint);
        canvas.drawLine(rect.right, rect.top + r, rect.right, rect.top + length, paint);
        canvas.drawLine(rect.right - r, rect.top, rect.right - length, rect.top, paint);
        //左下角
        canvas.drawArc(new RectF(rect.left, rect.bottom - d, rect.left + d, rect.bottom), 90, 90, false, paint);
        canvas.drawLine(rect.left, rect.bottom - r, rect.left, rect.bottom - length, paint);
        canvas.drawLine(rect.left + r, rect.bottom, rect.left + length, rect.bottom, paint);
        //右下角
        canvas.drawArc(new RectF(rect.right - d, rect.bottom - d, rect.right, rect.bottom), 0, 90, false, paint);
        canvas.drawLine(rect.right, rect.bottom - r, rect.right, rect.bottom - length, paint);
        canvas.drawLine(rect.right - r, rect.bottom, rect.right - length, rect.bottom, paint);
    }

    private void onMeasureScanBoxBounds() {
        if (remeasure) {
            float w = getWidth();
            float h = getHeight();
            float scale = 8.5f / 5.35f;
            if (w / h > scale) {//宽大于高 以高为基准设置扫描框

                float height = h - WindowUtil.dip2px(getContext(), 50f);
                float width = height * scale;
                rect.left = (int) ((w - width) / 2);
                rect.top = (int) ((h - height) / 2);
                rect.right = (int) (w - rect.left);
                rect.bottom = (int) (h - rect.top);

            } else {//以宽为基准设置扫描框

                float width = w - WindowUtil.dip2px(getContext(), 80f);
                float height = width / scale;
                rect.left = (int) ((w - width) / 2);
                rect.top = (int) ((h - height) / 2);
                rect.right = (int) (w - rect.left);
                rect.bottom = (int) (h - rect.top);

            }
            avatarBounds.set(Bounds.avatar(rect));
            nationalEmblemBounds.set(Bounds.nationalEmblem(rect));
            remeasure = false;
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        remeasure = true;
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    public void showErrorBounds(int info) {
        isShowErrorBounds = true;
        errorCode = info;
        invalidate();
    }
}
